package task11;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/27 8:49
 * @description:HashMap集合添加元素、遍历
 */
public class Take02 {
  public static void main(String[] args) {
    //1、创建HashMap<String,Student>对象遍历输出
    HashMap<String,Student> studentHashMap=new HashMap<String, Student>();
    //2、添加元素
    Student zs=new Student("张三",18);
    Student ls=new Student("里斯",19);
    Student ww=new Student("王五",17);
    studentHashMap.put("s1",zs);
    studentHashMap.put("s2",ls);
    studentHashMap.put("s3",ww);
    //3、遍历
    //通过键集合来获取值
    Iterator iterator=studentHashMap.keySet().iterator();
    while (iterator.hasNext()){
      //每一个键
      String key=(String) iterator.next();
      //根据键来获取值
      Student value=studentHashMap.get(key);
      System.out.println(key+"："+value);
    }
    System.out.println();

    //通过键值对集合来获取键和值
    Iterator iterator1=studentHashMap.entrySet().iterator();
    while (iterator1.hasNext()){
      //每一个键值对
      Entry<String,Student> entry= (Entry<String, Student>) iterator1.next();
      //通过键值对获取键和值
      String key=entry.getKey();
      Student value=entry.getValue();
      System.out.println(key+"--"+value);
    }

  }

}
